package com.cv4j.app.fragment;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.ability.pixels.ArithmeticAndLogicOperationAbility;
import com.cv4j.app.ability.pixels.FlipAbility;
import com.cv4j.app.ability.pixels.PrincipalColorExtractorAbility;
import com.cv4j.app.ability.pixels.ResizeAbility;
import com.cv4j.app.ability.pixels.RotateAbility;
import com.cv4j.app.ability.pixels.SubImageAbility;
import com.cv4j.app.app.BaseFragment;
import com.cv4j.app.utils.ClickUtil;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

/**
 * PixelOperatorFragment
 *
 * @since 2021-05-14
 */
public class PixelOperatorFragment extends BaseFragment implements Component.ClickedListener {
    private static final String TAG = "PixelOperatorFragment";
    private Text text1;
    private Text text2;
    private Text text3;
    private Text text4;
    private Text text5;
    private Text text6;
    private Context mContext;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component root = scatter.parse(ResourceTable.Layout_fragment_pixel_operator, container, false);
        mContext = container.getContext();
        initView(root);
        return root;
    }

    private void initView(Component root) {
        text1 = (Text) root.findComponentById(ResourceTable.Id_pixel_text1);
        text1.setClickedListener(this);
        text2 = (Text) root.findComponentById(ResourceTable.Id_pixel_text2);
        text2.setClickedListener(this);
        text3 = (Text) root.findComponentById(ResourceTable.Id_pixel_text3);
        text3.setClickedListener(this);
        text4 = (Text) root.findComponentById(ResourceTable.Id_pixel_text4);
        text4.setClickedListener(this);
        text5 = (Text) root.findComponentById(ResourceTable.Id_pixel_text5);
        text5.setClickedListener(this);
        text6 = (Text) root.findComponentById(ResourceTable.Id_pixel_text6);
        text6.setClickedListener(this);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_pixel_text1: // 算术操作，逻辑操作
                if (ClickUtil.isFastClick()) {
                    return;
                }
                jumpToAbility(ArithmeticAndLogicOperationAbility.class, (Text) component);
                break;
            case ResourceTable.Id_pixel_text2: // 提取图像中的ROI
                if (ClickUtil.isFastClick()) {
                    return;
                }
                jumpToAbility(SubImageAbility.class, (Text) component);
                break;
            case ResourceTable.Id_pixel_text3: // 提取图像中的主色彩
                if (ClickUtil.isFastClick(1000)) {
                    return;
                }
                jumpToAbility(PrincipalColorExtractorAbility.class, (Text) component);
                break;
            case ResourceTable.Id_pixel_text4: // 图像Rescale
                if (ClickUtil.isFastClick()) {
                    return;
                }
                jumpToAbility(ResizeAbility.class, (Text) component);
                break;
            case ResourceTable.Id_pixel_text5: // 图像翻转
                if (ClickUtil.isFastClick()) {
                    return;
                }
                jumpToAbility(FlipAbility.class, (Text) component);
                break;
            case ResourceTable.Id_pixel_text6: // 图像旋转
                if (ClickUtil.isFastClick()) {
                    return;
                }
                jumpToAbility(RotateAbility.class, (Text) component);
                break;
            default:
                break;
        }
    }

    /**
     * 属性类
     *
     * @param toClass 跳转的类名
     * @param text 文本
     */
    public void jumpToAbility(Class toClass, Text text) {
        if (mContext == null) {
            throw new NullPointerException("you are context is null");
        }
        Intent intent = new Intent();
        intent.setParam("Title", text.getText());
        intent.setOperation(new Intent.OperationBuilder()
            .withBundleName(mContext.getBundleName())
            .withAbilityName(toClass.getName())
            .build());

        // 换成下面context调用
        getFractionAbility().startAbility(intent);
    }
}
